#Copyright (c) 2007-2008 Travis Fickett (tfickett at ufl dot edu)
#
#Permission is hereby granted, free of charge, to any person
#obtaining a copy of this software and associated documentation
#files (the "Software"), to deal in the Software without
#restriction, including without limitation the rights to use,
#copy, modify, merge, publish, distribute, sublicense, and/or sell
#copies of the Software, and to permit persons to whom the
#Software is furnished to do so, subject to the following
#conditions:
#
#The above copyright notice and this permission notice shall be
#included in all copies or substantial portions of the Software.
#
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
#EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
#OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
#NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
#HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
#WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
#FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
#OTHER DEALINGS IN THE SOFTWARE.

#This class is not supposed to have any dependancys on tk so the
#app-dev can make his own methods to display the data
#the idea is to remove the class from pynavigate.py and replace it
#with something very very simple, that for now only takes this data
# and loads it into stringvars

##This is not done yet!!!


from pyNMEA import pyNMEAparser
import threading
import serial
import re,os
class PollSerial (threading.Thread):
    sPortName = "COM1"
    sBaudRate = "4800"
    sLogEnable = False
    sLogFilename = "seriallog.txt"

    sParser = pyNMEAparser()
    
    Run = 1

    def setPort(self, portName, baudRate):
        self.sPortName = portName
        self.sBaudRate = baudRate
    def enableLogging(self, fileName):
        self.sLogFilename = fileName
        self.sLogEnable = True

    def run(self):
        try:
            sHW = serial.Serial(port=self.sPortName, baudrate=self.BaudRate, timeout=1)
        except serial.SerialException:
            exit(1)
        while self.Run == 1:
            try:
                sLine = sHW.readline()
            except serial.SerialException:
                return
            else:
                sParser.parseSentence(sLine)
        sHW.close()
        